package com.test.io.thread;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;

public class TimeServer {
	
	
	
	public static void main(String[] args)throws Exception{
		int port=8181;
		if(args!=null&&args.length!=0){
			try {
				port=Integer.parseInt(args[0]);
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
		ServerSocket server=null;
		try {
			server=new ServerSocket(port);
			System.out.println("this time server is started in port:"+port);
			Socket socket=null;
			while(true){
				String threadName=Thread.currentThread().getId()+"--->"
						+Thread.currentThread().getName()+"--->"
						+Thread.currentThread().getThreadGroup().getName()+"--->";
				System.out.println("server thread==>"+threadName);
				socket=server.accept();
//				System.out.println("receive data...");
				new Thread(new TimeServerHandler(socket)).start();
//				System.out.println("handle thread is start");
				Thread.sleep(5000);
//				run(socket);
			}
			
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			System.out.println("server is over");
			if(server!=null){
				System.out.println("close server");
				server.close();
				server=null;
			}
		}
		
	}
	
	public static void run(Socket socket) {
		// TODO Auto-generated method stub
		if(socket==null)return;
//		BufferedReader in=null;
		DataInputStream in=null;
		DataOutputStream out=null;
//		PrintWriter out=null;
//		Writer out=null;
		try {
//			in=new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
			in=new DataInputStream(socket.getInputStream());
//			out=new PrintWriter(this.socket.getOutputStream(),true);
//			out=new OutputStreamWriter(socket.getOutputStream());
			out=new DataOutputStream(socket.getOutputStream());
			
			String currentTime=null;
			String body=null;
//			while(true){
//				body=in.readLine();
				body=in.readUTF();
				if(body==null){
					System.out.println("input is null ");
//					break;
				}
				System.out.println(new Date()+"the time server receive order :"+body);
				String code=body.split("_")[0];
				String threadName=Thread.currentThread().getId()+"--->"
						+Thread.currentThread().getName()+"--->"
						+Thread.currentThread().getThreadGroup().getName()+"--->";
				System.out.println(threadName+code+"---start sleep");
				Thread.sleep(10000);	
				System.out.println(code+"---finish sleep");
				currentTime="query time order".equalsIgnoreCase(body) 
						|| body.endsWith("query time order")?
						new Date().toString():"bad order ";
				
//				out.println(currentTime);
				out.writeUTF(currentTime);
				/*out.write(currentTime);
				out.flush();*/
						
//			}
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			if(in!=null){
				try {
					in.close();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			if(out!=null){
				try {
					out.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				out=null;
			}
			if(socket!=null){
				try {
					socket.close();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				socket=null;
			}
		}

	}

}
